/*
Hermes Core: General Purpose Mobile Agent Platform
Copyright (C) 2006 UNICAM - Università degli Studi di Camerino

Group leaders: Flavio Corradini and Emanuela Merelli

Developers: Ezio Bartocci, Diego Bonura, Rosario Culmone,
Davide Lorenzoli, Leonardo Mariani, Francesca Piersigilli, 
Lorenzo Scortichini, Leonardo Vito.

GNU Lesser Public License

This library is free software; you can redistribute it 
and/or modify it under the terms of the GNU Lesser General 
Public License as published by the Free Software Foundation, 
version 2.1 of the License.

This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Emanuela Merelli
Dipartimento di Matematica ed Informatica
Università di Camerino
via Madonna delle Carceri, 62032 Camerino, Italy
E.Mail: emanuela.merelli@unicam.it
*/
package hermesV2.core.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.StreamCorruptedException;


/**
 * It extends the <code> ObjectInputStream but it uses a specific <code>ClassLoader</code>.
 *
 * @author $author$
 *
 */
public class SmartObjectInputStream extends ObjectInputStream {
    /**
     * The specific classLoader.
    */
    private SmartClassLoader loader = null;

    /**
     * Creates a new SmartObjectInputStream object.
     *
     * @param is The input stream.
     *
     * @throws StreamCorruptedException Thrown when control information that was read from an object stream violates internal consistency checks.
     * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.
     * @throws IllegalArgumentException Thrown to indicate that a method has been passed an illegal or inappropriate argument.
     */
    public SmartObjectInputStream(InputStream is) throws StreamCorruptedException, IOException {
        super(is);

        if (is == null) {
            throw new IllegalArgumentException("Illegal null argument to SmartObjectInputStream");
        }
    }

    /**
     * Sets the specific <code>classLoader</code>.
     *
     * @param loader The <code> classLoader</code>.
     */
    public void setLoader(SmartClassLoader loader) {
        this.loader = loader;
    }

    /**
     * Resolves the class passes, if the loader is not seted it uses the primordial one otherwise the specific one.
     *
     * @param v Serialization's descriptor for classes. It contains the name and serialVersionUID of the class. The ObjectStreamClass for a specific class loaded in this Java VM can be found/created using the lookup method.
     *
     * @return The class resolved.
     *
     * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.
     * @throws ClassNotFoundException no definition for the class with the specified name could be found.
     */
    protected Class<?> resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException {
    	Class<?> cls=null;
        if (loader == null) {
            cls= super.resolveClass(v);
        } else {
            cls = loader.loadClass(v.getName(), v.getSerialVersionUID());

             
        }
        return cls;
    }
}
